import 'package:flutter/material.dart';
import 'package:pp_uikit/pp_group/pp_group_util.dart';
import 'package:pp_uikit/pp_switch.dart';
import 'package:pp_uikit/theme/pp_group_theme.dart';

///
/// Switch类型录入项
///
/// 包括"标题"、"Switch"
///
// ignore: must_be_immutable
class PPGroupSwitch extends StatefulWidget {
  /// 标题
  final String title;

  final bool value;

  /// 回调
  final ValueChanged<bool>? onChanged;

  /// 背景色
  final Color? backgroundColor;
  final Color? activeTrackColor;

  /// form配置
  PPGroupTheme? themeData;

  PPGroupSwitch({
    Key? key,
    this.title = "",
    this.value = false,
    this.onChanged,
    this.backgroundColor,
    this.activeTrackColor,
    this.themeData,
  }) : super(key: key) {
    themeData ??= PPGroupTheme();
  }

  @override
  State<StatefulWidget> createState() {
    return PPGroupSwitchState();
  }
}

class PPGroupSwitchState extends State<PPGroupSwitch> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.themeData?.backgroundColor,
      padding: widget.themeData?.groupPadding,
      child: ConstrainedBox(
        constraints: const BoxConstraints(
          minHeight: 40,
          maxHeight: 40,
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            // 主标题
            PPGroupUtil.buildTitleWidget(widget.title),

            // 显示右边Icon
            PPSwitch(
              value: widget.value,
              width: 52,
              height: 28,
              activeTrackColor: widget.activeTrackColor,
              onChanged: widget.onChanged,
            ),
          ],
        ),
      ),
    );
  }
}
